//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.model.wills.PssProductStock;
import com.aidong.model.wills.PssProductStockPK;
import com.aidong.model.wills.PssUseOrder;
import com.aidong.model.wills.PssUseOrderList;
import com.aidong.model.wills.PssUseOrderTotal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class PssUseOrderDao extends BaseDao<PssUseOrder, Integer> {
    public PssUseOrderDao() {
    }

    public void getLstUseOrder(Pagination<PssUseOrderTotal> pagination, ConditionMap cd) {
        ArrayList params = new ArrayList();
        String sqlw = cd.toSql(params);
        String sql = "select a.UseShop, a.UseType, b.ProductCode, c.Unit, sum(b.Quantity) as qty \nfrom PssUseOrder a inner join PssUseOrderList b on a.UseId=b.UseId \nleft join PssProductCode c on b.ProductCode=c.ProductCode \n" + (sqlw.length() > 0?"where " + sqlw:"");
        sql = sql + "  group by a.UseShop,a.UseType,b.ProductCode,c.Unit \n  having sum(b.Quantity)<>0 \norder by a.UseShop,b.ProductCode \n";
        this.queryPageListBySql(PssUseOrderTotal.class, pagination, sql, params.toArray());
    }

    public void getLstUseOrderList(Pagination<PssUseOrderTotal> pagination, ConditionMap cd) {
        ArrayList params = new ArrayList();
        String sqlw = cd.toSql(params);
        String sql = "select a.UseShop, a.UseDate, a.CreateUser, a.UseType, a.Note, b.ProductCode, c.Unit, b.Quantity as qty \nfrom PssUseOrder a inner join PssUseOrderList b on a.UseId=b.UseId \nleft join PssProductCode c on b.ProductCode=c.ProductCode \n" + (sqlw.length() > 0?"where " + sqlw:"") + " order by a.UseShop,b.ProductCode \n";
        this.queryPageListBySql(PssUseOrderTotal.class, pagination, sql, params.toArray());
    }

    public void addUseOrder(PssUseOrder pssUseOrder, List<PssUseOrderList> pssUseOrderList, PssProductStockDao pssProductStockDao) {
        this.add(pssUseOrder);
        Iterator i$ = pssUseOrderList.iterator();

        while(i$.hasNext()) {
            PssUseOrderList pss = (PssUseOrderList)i$.next();
            pss.setUseId(pssUseOrder.getUseId());
            this.getSession().save(pss);
            this.updateStock(pssProductStockDao, pssUseOrder.getUseShop(), pss.getProductCode(), pss.getQuantity(), pss.getCreateUser());
        }

    }

    private void updateStock(PssProductStockDao pssProductStockDao, String shopCode, String productCode, Double quantity, String createUser) {
        PssProductStockPK pssProductStockPK = new PssProductStockPK();
        pssProductStockPK.setProductCode(productCode);
        pssProductStockPK.setShopCode(shopCode);
        PssProductStock pssProductStock = (PssProductStock)pssProductStockDao.getById(pssProductStockPK);
        if(pssProductStock == null) {
            pssProductStock = new PssProductStock();
            pssProductStock.setProductCode(productCode);
            pssProductStock.setShopCode(shopCode);
            pssProductStock.setQuantity(quantity);
            pssProductStock.setCreateUser(createUser);
            pssProductStock.setCreateTime(new Date());
            pssProductStock.setUpdateTime(new Date());
            pssProductStockDao.add(pssProductStock);
        } else {
            pssProductStock.setQuantity(Double.valueOf(pssProductStock.getQuantity().doubleValue() - quantity.doubleValue()));
            pssProductStock.setUpdateTime(new Date());
            pssProductStockDao.update(pssProductStock);
        }

    }
}
